package com.itheima.service;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: bowen
 * @Date: 2019/7/20 15:13
 * @Description:
 */
@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements  MemberService {

	@Autowired
	MemberDao memberDao;

	@Override
	public Member findByTelephone(String telephone) {
		return memberDao.findByTelephone(telephone);
	}

	@Override
	public void add(Member member) {
		// 判断用户是否带有密码，如果带有密码，需要加密
		if(!StringUtils.isEmpty(member.getPassword())){
			String md5 = MD5Utils.md5(member.getPassword());
			member.setPassword(md5);
		}
		memberDao.add(member);
	}

	/**
	 *  @描述：根据月份查询会员数量
	 */
	public List<Integer> findCountByMonth(List<String> months) {
		List<Integer> counts = new ArrayList<>();
		for (String month : months) {
			// 2019-07
		//	计算最后一天的日期
		//	2019-07-31
			String end = month+"-31";
			Integer countByMonth = memberDao.findCountByMonth(end);
			counts.add(countByMonth);
		}
		return counts;
	}
}
